package com.zmn.oms.zmn.normal.business.impl.serviceitem;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.base.plat.engine.common.constant.DubboConsts;
import com.zmn.base.price.common.constant.ItemAdditionConst;
import com.zmn.base.price.common.dto.item.ItemCellPriceDTO;
import com.zmn.base.price.common.dto.item.ItemTotalPriceDRO;
import com.zmn.base.price.common.dto.item.ItemTotalPriceQuery;
import com.zmn.base.price.dubbo.interfaces.calculate.ItemPriceRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.business.interfaces.discount.OrderDiscountBService;
import com.zmn.oms.business.interfaces.messageV1.common.MessageSendBaseService;
import com.zmn.oms.business.interfaces.product.OrderProductExtendBService;
import com.zmn.oms.business.interfaces.serviceitem.OrderServiceItemBService;
import com.zmn.oms.business.interfaces.servtype.ServItemBService;
import com.zmn.oms.common.annotation.OrderWorkCasOperate;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.acceptance.OrderWorkAcceptanceDTO;
import com.zmn.oms.model.dto.fault.FaultItemDTO;
import com.zmn.oms.model.dto.fault.FaultItemGroupDTO;
import com.zmn.oms.model.dto.serviceitem.*;
import com.zmn.oms.model.dto.work.modify.ServiceItemDetailResultDTO;
import com.zmn.oms.model.dto.work.modify.ServiceItemQueryDTO;
import com.zmn.oms.model.dto.work.modify.ServiceItemResultDTO;
import com.zmn.oms.model.entity.order.OrderExtend;
import com.zmn.oms.model.entity.product.OrderProduct;
import com.zmn.oms.model.entity.product.OrderProductExtend;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItem;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItemCell;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.model.vo.work.modify.amount.ZsServiceItemVO;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.discount.OrderDiscountService;
import com.zmn.oms.services.interfaces.order.OrderExtendService;
import com.zmn.oms.services.interfaces.product.OrderProductService;
import com.zmn.oms.services.interfaces.serviceitem.OrderServiceItemService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.impl.serviceItem.ZsOrderServiceItemBServiceImpl;
import com.zmn.oms.zmn.business.interfaces.acceptance.OrderWorkAcceptanceBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import com.zmn.oms.zmn.normal.business.interfaces.serviceitem.ZsNormalOrderServiceItemBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 类描述：啄木鸟-普通工单-服务项
 *
 * @author heciqi
 * @date 2018/12/06 14:46
 */
@Slf4j
@Service
public class ZsNormalOrderServiceItemBServiceImpl extends ZsOrderServiceItemBServiceImpl implements ZsNormalOrderServiceItemBService {

	@Autowired
	protected OrderProductExtendBService orderProductExtendBService;
	@Autowired
	protected OrderServiceItemService orderServiceItemService;
	@Autowired
	protected OrderServiceItemBService orderServiceItemBService;
	@Autowired
	protected OrderWorkService orderWorkService;
	@Autowired
	protected OrderDetailService orderDetailService;
	@Autowired
	protected OrderProductService orderProductService;
	@Autowired
	protected ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
	@Autowired
	protected OrderDiscountBService orderDiscountBService;
	@Autowired
	protected OrderDiscountService orderDiscountService;
	@Autowired
	private ServItemBService servItemBService;
	@Autowired
	protected OrderExtendService orderExtendService;
	@Autowired
	private MessageSendBaseService messageSendBaseService;
	@Autowired
	private OrderWorkAcceptanceBService orderWorkAcceptanceBService;

	@Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
	protected ItemPriceRemoteService itemPriceRemoteService;

	@Override
	public List<ZsServiceItemVO> findServiceItemVo(Long orderId, Long workId) throws OmsBaseException {

		// 工单
		OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
		if (Objects.isNull(orderWork)) {
			return Collections.EMPTY_LIST;
		}
		// 查询订单产品扩展
		List<OrderProductExtend> orderProductExtendList = orderProductExtendBService.getOrderProductExtend(orderId);
		// 订单服务项
		List<OrderServiceItem> orderServiceItemList = orderServiceItemService.listByOrderId(orderId);

		List<ZsServiceItemVO> serviceItemVOList = Lists.newArrayList();
		Map<Long, List<OrderServiceItem>> serviceItemMap = orderServiceItemList.stream().collect(Collectors.groupingBy(OrderServiceItem::getProExtId));
		orderProductExtendList.forEach(orderProductExtend -> {
			ZsServiceItemVO serviceItemVO = new ZsServiceItemVO();
			serviceItemVO.setOrderId(orderId);
			serviceItemVO.setWorkId(workId);
			serviceItemVO.setProductId(orderProductExtend.getProductId());
			serviceItemVO.setProductName(orderProductExtend.getProductName());
			serviceItemVO.setProExtId(orderProductExtend.getProExtId());
			serviceItemVO.setServiceItemList(serviceItemMap.get(orderProductExtend.getProExtId()));
			serviceItemVO.setServItemType(orderWork.getServItemType());

			if (!CollectionUtils.isEmpty(serviceItemVO.getServiceItemList())) {
				int sum = serviceItemVO.getServiceItemList().stream().mapToInt(item1 -> item1.getTotalPrice()).sum();
				serviceItemVO.setTotalAmount(sum);
			} else {
				serviceItemVO.setTotalAmount(0);
			}

			serviceItemVOList.add(serviceItemVO);
		});

		return serviceItemVOList;
	}

	@Override
	public List<ServiceItemGroupDTO> findServiceitemList(Long orderId, Long workId, Integer productId) throws OmsBaseException {

		OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
		// 一个工单只可以有一个产品，所以可以这么取
		List<OrderProduct> products = orderProductService.listOrderProductByOrderId(orderId);
		// 特殊调整项目, 其他服务项， 免费服务项, 远程费
		List<Integer> commonIdList = Lists.newArrayList();
		commonIdList.add(ItemAdditionConst.ADDITION_ADJUST);
		commonIdList.add(ItemAdditionConst.ADDITION_REMOTE);

		ServiceItemQueryDTO serviceItemQueryDTO = ServiceItemQueryDTO
				.builder()
				.servItemType(orderWork.getServItemType())
				.channelId(orderWork.getChannelId())
				.cityId(orderWork.getCityId())
				.bizType(orderWork.getBizType())
				.productId(productId)
				.brandId(products.get(0).getBrandId())
				.idList(commonIdList)
				.tariffId(products.get(0).getTariffId())
				.servItemType(orderWork.getServItemType())
				.searchAdditionItem(true)
				.build();
		List<ServiceItemResultDTO> serviceItemResultDTOList = servItemBService.listServiceItem(serviceItemQueryDTO);

		if (CollectionUtil.isNullOrEmpty(serviceItemResultDTOList)) {
			return Lists.newArrayList();
		}

		Map<ServiceItemGroupDTO, List<ServiceItemResultDTO>> serviceItemGroupDTOListMap = serviceItemResultDTOList.stream()
				.collect(Collectors.groupingBy(itemDRO -> {
					ServiceItemGroupDTO serviceItemGroupDTO = new ServiceItemGroupDTO();
					serviceItemGroupDTO.setServItemGroupId(itemDRO.getItemCategId());
					serviceItemGroupDTO.setServItemGroupName(itemDRO.getItemCategName());
					return serviceItemGroupDTO;
				}));

		serviceItemGroupDTOListMap.forEach((key, value) -> {
			List<ServiceItemDTO> serviceItemDTOS = BeanMapper.mapList(value, ServiceItemDTO.class);
			key.setServiceItemDTOList(serviceItemDTOS);
		});
		return Lists.newArrayList(serviceItemGroupDTOListMap.keySet());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@OrderWorkCasOperate(type = OrderConsts.ORDER_OP_TYPE_SERVICE_ITEM)
	@ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_SERVICEITEM)
	public ResultDTO saveServiceitem(AddOrderServiceItemDTO addOrderServiceItemDTO) throws OmsBaseException {
		return doSaveServiceitem(addOrderServiceItemDTO, null);
	}

	@Override
	public Integer getDiscount(AddOrderServiceItemDTO addOrderServiceItemDTO) throws OmsBaseException {
		return doGetDiscount(addOrderServiceItemDTO,null);
	}

	@Override
	public List<FaultItemGroupDTO> findFaultItemList(Long orderId, Long workId, Integer productId) throws OmsBaseException {

		OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
		List<OrderProduct> products = orderProductService.listOrderProductByOrderId(orderId);

		ServiceItemQueryDTO serviceItemQueryDTO = ServiceItemQueryDTO
				.builder()
				.servItemType(orderWork.getServItemType())
				.brandId(products.get(0).getBrandId())
				.tariffId(products.get(0).getTariffId())
				.channelId(orderWork.getChannelId())
				.cityId(orderWork.getCityId())
				.productId(productId)
				.bizType(orderWork.getBizType())
				.searchAdditionItem(true)
				.build();

		OrderExtend orderExtend = orderExtendService.findOrderExtendByOrderId(orderWork.getOrderId());
		if (Objects.nonNull(orderExtend) && NumberUtil.isNotNullOrZero(orderExtend.getDifficultRatioId())) {
			serviceItemQueryDTO.setDifficultId(orderExtend.getDifficultRatioId());
		}

		List<ServiceItemResultDTO> serviceItemResultDTOList = servItemBService.listServiceItem(serviceItemQueryDTO);

		if (CollectionUtil.isNullOrEmpty(serviceItemResultDTOList)) {
			return Lists.newArrayList();
		}

		Map<FaultItemGroupDTO, List<ServiceItemResultDTO>> serviceItemGroupDTOListMap = serviceItemResultDTOList.stream()
				.collect(Collectors.groupingBy(productFaultItemDRO -> {
					FaultItemGroupDTO faultItemGroupDTO = new FaultItemGroupDTO();
					faultItemGroupDTO.setServItemGroupId(productFaultItemDRO.getItemCategId());
					faultItemGroupDTO.setServItemGroupName(productFaultItemDRO.getItemCategName());
					return faultItemGroupDTO;
				}));

		serviceItemGroupDTOListMap.forEach((key, value) -> {
			List<FaultItemDTO> serviceItemDTOS = BeanMapper.mapList(value, FaultItemDTO.class);
			key.setServiceItemDTOList(serviceItemDTOS);
		});

		return Lists.newArrayList(serviceItemGroupDTOListMap.keySet());
	}

	@Override
	public Integer listTieredTotalPrice(TieredTotalPriceDTO tieredTotalPriceDTO){

		if (CollectionUtil.isNullOrEmpty(tieredTotalPriceDTO.getDetailList())) {
			return tieredTotalPriceDTO.getPrice() * tieredTotalPriceDTO.getNumber();
		}

		List<ItemCellPriceDTO> itemCells = tieredTotalPriceDTO.getDetailList()
				.stream()
				.map(e -> new ItemCellPriceDTO(e.getItemCellId(), e.getCellPrice(), 0))
				.collect(Collectors.toList());

		List<ItemTotalPriceQuery> itemPriceList = new ArrayList<>();
		ItemTotalPriceQuery build = ItemTotalPriceQuery
				.builder()
				.itemId(tieredTotalPriceDTO.getItemId())
				.number(tieredTotalPriceDTO.getNumber())
				.itemCells(itemCells)
				.build();
		itemPriceList.add(build);
		log.info("#oms#orderId={},获取获取阶梯价金额 入参：{}", tieredTotalPriceDTO.getOrderId(), JSON.toJSONString(itemPriceList));
		ResponseDTO<List<ItemTotalPriceDRO>> resp = itemPriceRemoteService.listTotalPriceByQuery(itemPriceList);
		log.info("#oms#orderId={},获取获取阶梯价金额 出参：{}", tieredTotalPriceDTO.getOrderId(), JSON.toJSONString(resp));
		List<ItemTotalPriceDRO> itemPriceDRO = resp.getData();
		if (resp.isSuccess() && CollectionUtil.isNotNullOrEmpty(itemPriceDRO)) {
			return itemPriceDRO.get(0).getTotalPrice();
		}
		return tieredTotalPriceDTO.getPrice() * tieredTotalPriceDTO.getNumber();
	}

	/**
	 * 修改服务项
	 * @param addOrderServiceItemDTO
	 * @param dbOrderWork
	 * @return
	 * @throws OmsBaseException
	 */
	private ResultDTO doSaveServiceitem(AddOrderServiceItemDTO addOrderServiceItemDTO,
										OrderWork dbOrderWork) throws OmsBaseException {
		log.info("添加服务项1 doSaveServiceitem addOrderServiceItemDTO={}", JSON.toJSONString(addOrderServiceItemDTO));
		// 检查权限
		Long orderId = addOrderServiceItemDTO.getOrderId();
		Long workId = addOrderServiceItemDTO.getWorkId();
		if (addOrderServiceItemDTO.isVerifyPermission()) {
			OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(orderId, workId,
					OrderConsts.ORDER_OP_TYPE_SERVICE_ITEM, addOrderServiceItemDTO.getQuotationType());
			if (!operatePermission.getCanOperate()) {
				throw new OmsBaseException(operatePermission.getMsg());
			}
		}

		// 故障服务项订单检查
		if (dbOrderWork == null) {
			dbOrderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
		}
		Integer originalServItemType = Optional.ofNullable(addOrderServiceItemDTO.getServItemType()).orElse(dbOrderWork.getServItemType());
		// 兼容老逻辑没有ServItemType的情况
		addOrderServiceItemDTO.setServItemType(originalServItemType);
		if (!Objects.equals(dbOrderWork.getServItemType(), originalServItemType)) {
			throw new OmsBaseException(StatusConsts.ERROR_DATA, "订单的服务项类型不匹配！");
		}

		// 校验服务项
		servItemBService.verifyServiceItem(addOrderServiceItemDTO.getOrderServiceItemList());

		// 获取最新的服务项价格
		List<OrderServiceItem> serviceItemList = this.progressOrderServiceItemPriceList(addOrderServiceItemDTO, dbOrderWork);
		log.info("添加服务项2 serviceItemList={}", JSON.toJSONString(serviceItemList));

		// 过滤服务项-计价器3.0订单只保存价格最高的维修项目
		List<OrderServiceItem> orderServiceItemList = servItemBService.filterServiceItem(serviceItemList, addOrderServiceItemDTO.getServItemType(), dbOrderWork.getFinalPrice());
		addOrderServiceItemDTO.setOrderServiceItemList(orderServiceItemList);

		log.info("添加服务项3 addOrderServiceItemDTO={}", JSON.toJSONString(addOrderServiceItemDTO));

		long startTime = System.currentTimeMillis();
		ResultDTO resultDTO = orderServiceItemBService.insertServiceItem(addOrderServiceItemDTO);
		long endTime = System.currentTimeMillis();
		log.info("insertServiceItem 接口总耗时：{}", endTime - startTime);

		// 改价格删除尾款验收，需要工程师重新发起尾款验收
		OrderWorkAcceptanceDTO dto = new OrderWorkAcceptanceDTO();
		dto.setOrderId(addOrderServiceItemDTO.getOrderId());
		dto.setWorkId(addOrderServiceItemDTO.getWorkId());
		dto.setOperator(addOrderServiceItemDTO.getOperator());
		dto.setOperatorId(addOrderServiceItemDTO.getOperatorId());
		dto.setOperatorType(addOrderServiceItemDTO.getOperatorType());
		orderWorkAcceptanceBService.clearPaymentAcceptance(dto, dbOrderWork);

		return resultDTO;
	}

	/**
	 * 获取优惠价
	 * @param addOrderServiceItemDTO
	 * @param dbOrderWork
	 * @return
	 * @throws OmsBaseException
	 */
	private Integer doGetDiscount(AddOrderServiceItemDTO addOrderServiceItemDTO,
										OrderWork dbOrderWork) throws OmsBaseException {
		log.info("获取优惠价1 doGetDiscount addOrderServiceItemDTO={}", JSON.toJSONString(addOrderServiceItemDTO));
		// 检查权限
		Long orderId = addOrderServiceItemDTO.getOrderId();
		Long workId = addOrderServiceItemDTO.getWorkId();
		OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(orderId, workId,
				OrderConsts.ORDER_OP_TYPE_SERVICE_ITEM, addOrderServiceItemDTO.getQuotationType());
		if (!operatePermission.getCanOperate()) {
			throw new OmsBaseException(operatePermission.getMsg());
		}

		// 故障服务项订单检查
		if (dbOrderWork == null) {
			dbOrderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
		}
		Integer originalServItemType = Optional.ofNullable(addOrderServiceItemDTO.getServItemType()).orElse(dbOrderWork.getServItemType());
		// 兼容老逻辑没有ServItemType的情况
		addOrderServiceItemDTO.setServItemType(originalServItemType);
		if (!Objects.equals(dbOrderWork.getServItemType(), originalServItemType)) {
			throw new OmsBaseException(StatusConsts.ERROR_DATA, "订单的服务项类型不匹配！");
		}

		// 校验服务项
		servItemBService.verifyServiceItem(addOrderServiceItemDTO.getOrderServiceItemList());

		// 获取最新的服务项价格
		List<OrderServiceItem> serviceItemList = this.progressOrderServiceItemPriceList(addOrderServiceItemDTO, dbOrderWork);
		log.info("获取优惠价2 serviceItemList={}", JSON.toJSONString(serviceItemList));

		// 过滤服务项-计价器3.0订单只保存价格最高的维修项目
		List<OrderServiceItem> orderServiceItemList = servItemBService.filterServiceItem(serviceItemList, addOrderServiceItemDTO.getServItemType(), dbOrderWork.getFinalPrice());
		addOrderServiceItemDTO.setOrderServiceItemList(orderServiceItemList);

		log.info("获取优惠价3 addOrderServiceItemDTO={}", JSON.toJSONString(addOrderServiceItemDTO));

		Integer discount = orderServiceItemBService.getDiscount(addOrderServiceItemDTO);

		return discount;
	}

	/**
	 * 服务项价格处理
	 *
	 * @param addOrderServiceItemDTO
	 * @param orderWork
	 * @return
	 */
	private List<OrderServiceItem> progressOrderServiceItemPriceList(AddOrderServiceItemDTO addOrderServiceItemDTO,
																	 OrderWork orderWork) throws OmsBaseException{

		List<OrderServiceItem> orderServiceItemList = addOrderServiceItemDTO.getOrderServiceItemList();
		Long orderId = orderWork.getOrderId();

		List<OrderProduct> productList = orderProductService.listOrderProductByOrderId(orderId);
		OrderExtend orderExtend = orderExtendService.findOrderExtendByOrderId(orderId);

		List<Integer> commonIdList = Lists.newArrayList();
		commonIdList.add(ItemAdditionConst.ADDITION_ADJUST);
		commonIdList.add(ItemAdditionConst.ADDITION_REMOTE);
		commonIdList.add(ItemAdditionConst.ADDITION_FREE);

		// 查询服务项数据
		ServiceItemQueryDTO serviceItemQueryDTO = ServiceItemQueryDTO
				.builder()
				.cityId(orderWork.getCityId())
				.channelId(orderWork.getChannelId())
				.productId(orderWork.getProductId())
				.bizType(orderWork.getBizType())
				.servItemType(addOrderServiceItemDTO.getServItemType())
				.searchAdditionItem(true)
				.idList(commonIdList)
				.build();

		if (CollectionUtils.isNotEmpty(productList)) {
			OrderProduct orderProduct = productList.get(0);
			serviceItemQueryDTO.setBrandId(orderProduct.getBrandId());
			serviceItemQueryDTO.setTariffId(orderProduct.getTariffId());
		}
		if (Objects.nonNull(orderExtend) && NumberUtil.isNotNullOrZero(orderExtend.getDifficultRatioId())) {
			serviceItemQueryDTO.setDifficultId(orderExtend.getDifficultRatioId());
		}

		log.info("获取故障项 入参：{}", JSON.toJSONString(serviceItemQueryDTO));
		List<ServiceItemResultDTO> serviceItemResultDTOList = servItemBService.listServiceItem(serviceItemQueryDTO);
		log.info("获取故障项 出参：{}", JSON.toJSONString(serviceItemResultDTOList));
		Map<Integer, ServiceItemResultDTO> faultItemDROMap = serviceItemResultDTOList.stream().collect(Collectors.toConcurrentMap(ServiceItemResultDTO::getItemId, faultItem -> faultItem, (v1, v2) -> v1));

		List<OrderServiceItem> serviceItemList = Lists.newArrayListWithExpectedSize(orderServiceItemList.size());
		orderServiceItemList.forEach(orderServiceItem -> {
			ServiceItemResultDTO faultItemDRO = faultItemDROMap.get(orderServiceItem.getServItemId());

			// 一口价订单、计价器3.0 上门费跳过过滤
			if (Objects.equals(orderWork.getFinalPrice(), GlobalConsts.YES) || Objects.equals(orderWork.getServItemType(), OrderConsts.SERVICE_ITEM_TYPE_QUOTATION_THREE)) {
				if ("上门费".equals(orderServiceItem.getServItemName())) {
					serviceItemList.add(orderServiceItem);
					return;
				}
			}

			if (Objects.isNull(faultItemDRO)) {
				log.info("订单[{}]服务项不存在：{}", orderId, JSON.toJSONString(orderServiceItem));
				return;
			}

			serviceItemList.add(orderServiceItem);

			if (Objects.equals(GlobalConsts.YES, faultItemDRO.getPriceStandard())) {
				// 一口价
				// 如果价格为null或0，则使用调用方传过来的价格
				if (NumberUtil.isNotNullOrZero(faultItemDRO.getPrice())) {
					orderServiceItem.setItemPrice(faultItemDRO.getPrice());
				}
				orderServiceItem.setExternalSettlementPrice(faultItemDRO.getExternalSettlementPrice());
				orderServiceItem.setInternalSettlementPrice(faultItemDRO.getInternalSettlementPrice());
			} else {
				// 非一口价
				if (orderServiceItem.getItemPrice() == null) {
					orderServiceItem.setItemPrice(0);
				}
				orderServiceItem.setExternalSettlementPrice(orderServiceItem.getExternalSettlementPrice());
				orderServiceItem.setInternalSettlementPrice(orderServiceItem.getInternalSettlementPrice());
			}

			if (!servItemBService.isQuotationType(addOrderServiceItemDTO.getServItemType())) {
				return;
			}

			// 计价器订单，保存服务项明细
			if (CollectionUtil.isNotNullOrEmpty(faultItemDRO.getDetailResultDTOList())) {
				List<OrderServiceItemCell> itemDetailList = new ArrayList<>(faultItemDRO.getDetailResultDTOList().size());
				for (ServiceItemDetailResultDTO detailResultDTO : faultItemDRO.getDetailResultDTOList()) {
					OrderServiceItemCell cell = new OrderServiceItemCell();
					cell.setOrderId(orderId);
					cell.setWorkId(orderWork.getWorkId());
					cell.setServItemId(orderServiceItem.getServItemId());
					cell.setProExtId(addOrderServiceItemDTO.getProExtId());
					cell.setItemCellId(detailResultDTO.getItemCellId());
					cell.setItemCellName(detailResultDTO.getItemCellName());
					cell.setCellPrice(detailResultDTO.getCellPrice());
					cell.setSort(detailResultDTO.getSort());
					itemDetailList.add(cell);
				}
				orderServiceItem.setItemDetailList(itemDetailList);
			}

			// 如果存在阶梯价并且数量大于1
			if (NumberUtil.isNotNullOrZero(orderServiceItem.getTieredTotalPrice()) && orderServiceItem.getNumber() > 1) {
				// 处理内部价阶梯价，如果内部价为空，则直接使用销售价阶梯总价。否则计算内部价阶梯总价
				if (Objects.isNull(orderServiceItem.getInternalSettlementPrice())) {
					orderServiceItem.setInternalSettlementPrice(orderServiceItem.getItemPrice());
					orderServiceItem.setInternalTotalPrice(orderServiceItem.getTieredTotalPrice());
				} else {
					TieredTotalPriceDTO tieredTotalPriceDTO = new TieredTotalPriceDTO();
					tieredTotalPriceDTO.setOrderId(orderId);
					tieredTotalPriceDTO.setItemId(orderServiceItem.getServItemId());
					tieredTotalPriceDTO.setNumber(orderServiceItem.getNumber());
					tieredTotalPriceDTO.setPrice(orderServiceItem.getInternalSettlementPrice());

					List<OrderServiceItemCell> itemCellList = orderServiceItem.getItemDetailList();
					if (CollectionUtil.isNotNullOrEmpty(itemCellList)) {
						List<ServiceItemDetailDTO> detailList = new ArrayList<>(itemCellList.size());
						for (OrderServiceItemCell cell : itemCellList) {
							ServiceItemDetailDTO detailDTO = new ServiceItemDetailDTO();
							detailDTO.setItemCellId(cell.getItemCellId());
							detailDTO.setItemCellName(cell.getItemCellName());
							detailDTO.setCellPrice(cell.getCellPrice());
							detailDTO.setSort(cell.getSort());
							detailList.add(detailDTO);
						}
						tieredTotalPriceDTO.setDetailList(detailList);
					}
					orderServiceItem.setInternalTotalPrice(this.listTieredTotalPrice(tieredTotalPriceDTO));
				}

				// 处理外部价阶梯价，如果外部价为空，则直接使用销售价阶梯总价。否则计算外部价阶梯总价
				if (Objects.isNull(orderServiceItem.getExternalSettlementPrice())) {
					orderServiceItem.setExternalSettlementPrice(orderServiceItem.getItemPrice());
					orderServiceItem.setExternalTotalPrice(orderServiceItem.getTieredTotalPrice());
				} else {
					TieredTotalPriceDTO tieredTotalPriceDTO = new TieredTotalPriceDTO();
					tieredTotalPriceDTO.setOrderId(addOrderServiceItemDTO.getOrderId());
					tieredTotalPriceDTO.setItemId(orderServiceItem.getServItemId());
					tieredTotalPriceDTO.setNumber(orderServiceItem.getNumber());
					tieredTotalPriceDTO.setPrice(orderServiceItem.getExternalSettlementPrice());

					List<OrderServiceItemCell> itemCellList = orderServiceItem.getItemDetailList();
					if (CollectionUtil.isNotNullOrEmpty(itemCellList)) {
						List<ServiceItemDetailDTO> detailList = new ArrayList<>(itemCellList.size());
						for (OrderServiceItemCell cell : itemCellList) {
							ServiceItemDetailDTO detailDTO = new ServiceItemDetailDTO();
							detailDTO.setItemCellId(cell.getItemCellId());
							detailDTO.setItemCellName(cell.getItemCellName());
							detailDTO.setCellPrice(cell.getCellPrice());
							detailDTO.setSort(cell.getSort());
							detailList.add(detailDTO);
						}
						tieredTotalPriceDTO.setDetailList(detailList);
					}
					orderServiceItem.setExternalTotalPrice(this.listTieredTotalPrice(tieredTotalPriceDTO));
				}
			}
		});

		return serviceItemList;
	}

}
